package com.copro.chemdb.subject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ihelpuoo.db.sql.SQLSegment;
import org.ihelpuoo.db.sql.SQLUtils;
import org.ihelpuoo.lang.StringUtils;

import com.copro.simple.Application;

public class SaveSubjectAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Application app = Application.me(request);
		Subject subject = (Subject) form;
		String[] optionValues = request.getParameterValues("optionValues");
		String[] optionChecks = request.getParameterValues("optionChecks");
		// Old
		if (subject.getId() > 0) {
			SQLSegment sql = app.getSQL("updateSubject");
			sql.set("id", subject.getId());
			sql.set("code", subject.getCode());
			sql.set("name", subject.getName());
			sql.set("brief", subject.getBrief());
			sql.set("title", subject.getTitle());
			sql.set("type", subject.getType());
			sql.set("parentId", subject.getParentId() > 0 ? subject
					.getParentId() : "NULL");
			sql.set("quesId", subject.getQuesId() > 0 ? subject.getQuesId()
					: "NULL");
			sql.set("order", subject.getOrder());
			sql.set("pattern", subject.getPattern());
			sql.set("rule", subject.getRule());
			sql.set("enumType", subject.getEnumType());
			sql.add("canQuery", subject.isCanQuery() ? 1 : 0);
			sql.add("inques", subject.isInques() ? 1 : 0);
			sql.add("indb", subject.isIndb() ? 1 : 0);
			app.exec(sql.toString());
		} else {
			SQLSegment sql = app.getSQL("newSubject");
			sql.set("code", subject.getCode());
			sql.set("name", subject.getName());
			sql.set("brief", subject.getBrief());
			sql.set("title", subject.getTitle());
			sql.set("type", subject.getType());
			sql.set("parentId", subject.getParentId() > 0 ? subject
					.getParentId() : "NULL");
			sql.set("quesId", subject.getQuesId() > 0 ? subject.getQuesId()
					: "NULL");
			sql.set("order", subject.getOrder());
			sql.set("pattern", subject.getPattern());
			sql.set("rule", subject.getRule());
			sql.set("enumType", subject.getEnumType());
			sql.add("canQuery", subject.isCanQuery() ? 1 : 0);
			sql.add("inques", subject.isInques() ? 1 : 0);
			sql.add("indb", subject.isIndb() ? 1 : 0);
			app.insert(sql.toString(), "maxSubject", subject, "id");
		}
		// save options
		SQLSegment sql2 = app.getSQL("addSubjectOptions");
		SQLSegment sql = app.getSQL("clearSubjectOptions");
		sql.set("subjectId", subject.getId());
		app.exec(sql.toString());
		if (null != optionValues) {
			for (int i = 0; i < optionValues.length; i++) {
				if (StringUtils.isBlank(optionValues[i]))
					continue;
				sql2.clearAll();
				sql2.set("id", i);
				sql2.set("text", SQLUtils.escapeFieldValue(optionValues[i]));
				sql2.set("subjectId", subject.getId());
				sql2.set("isext", optionChecks[i]);
				app.exec(sql2.toString());
			}
		}

		SubjectUtils.setupVars(request, app, subject);
		return mapping.findForward("edit");
	}

}
